﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
using System.Web;
using System.Security.Principal;
using System.Threading;
using System.Collections;
using System.Management;
using System.Data;

namespace CMD_WEB.BAL
{
    public class Auth
    {
        public static CMD_WEB.BAL.JSONOBJ.UserInfo GetGruposUsuario()
        {
            CMD_WEB.BAL.JSONOBJ.UserInfo users;
            ArrayList groups = new ArrayList();
            string grupos = "";

            foreach (System.Security.Principal.IdentityReference group in System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
            {
                string tmp = group.Translate(typeof(System.Security.Principal.NTAccount)).ToString();
                if (tmp.Split('\\').Length > 1)
                {
                    grupos += tmp.Split('\\').Last().ToString() + ",";
                }
            }
            DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GETUSERGROUP'" + grupos + "'");
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {

                //string user = System.Environment.UserName;
                string user=HttpContext.Current.User.Identity.Name;
                users = new CMD_WEB.BAL.JSONOBJ.UserInfo(user, dt.Rows[0]["dsc_grupo"].ToString(), dt.Rows[0]["dsc_rol"].ToString());
                return users;
            }
        }
        
        //public static CMD_WEB.BAL.JSONOBJ.UserInfo GetRol()
        //{ 
        //    //CMD_WEB.BAL.JSONOBJ.RolInfo users;
        //    CMD_WEB.BAL.JSONOBJ.UserInfo roles;
        //    ArrayList groups = new ArrayList();
        //    string grupos = "";

        //    foreach (System.Security.Principal.IdentityReference group in System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
        //    {
        //        string tmp = group.Translate(typeof(System.Security.Principal.NTAccount)).ToString();
        //        if (tmp.Split('\\').Length > 1)
        //        {
        //            grupos += tmp.Split('\\').Last().ToString() + ",";
        //        }
        //    }
        //    DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec getUserGroup'" + grupos + "'");
        //    if (dt.Rows.Count == 0)
        //    {
        //        return null;
        //    }
        //    else
        //    {

        //        //string user = System.Environment.UserName;
        //        roles = new CMD_WEB.BAL.JSONOBJ.UserInfo("", "", dt.Rows[0]["dsc_rol"].ToString());
        //        return roles;
        //    }
        //}
   
      
    }
}
